package com.chuanjiabao.wisdom.util;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;

import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.activity.LoginActivity;

import org.json.JSONException;
import org.json.JSONObject;

public class StringUtil {

    //斜体
    public static SpannableString getSpanH(String s) {
        SpannableString string = new SpannableString(s);
        AbsoluteSizeSpan span = new AbsoluteSizeSpan(12, true);
        //粗斜体
        string.setSpan(span, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return string;
    }

    public static boolean isFail(String s) {
        try {
            JSONObject jsonObject = new JSONObject(s);
            int code = (int) jsonObject.get("code");
            return 1 == code;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void showFail(Context context, String s) {
        try {
            JSONObject json = new JSONObject(s);
            int code = (int) json.get("code");
            if (code == 10001) {
                SpUtils.clear(context);
                ToastUtil.showToast("登陆过期，请重新登陆");
                if (ActivityStack.getScreenManager()
                        .currentActivity()
                        .getClass() != LoginActivity.class) {
                    context.startActivity(new Intent(context, LoginActivity.class));
                    ActivityStack.getScreenManager().popAllActivityExceptOne(LoginActivity.class);
                }
            } else {
                String msg = (String) json.get("msg");
                ToastUtil.showToast(msg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public static void showFail(Context context, String s, boolean isShow) {
        try {
            JSONObject json = new JSONObject(s);
            int code = (int) json.get("code");
            if (code == 10001) {
                SpUtils.clear(context);
                ToastUtil.showToast("登陆过期，请重新登陆");
                context.startActivity(new Intent(context, LoginActivity.class));
                ActivityStack.getScreenManager().popAllActivityExceptOne(LoginActivity.class);
            } else {
                String msg = (String) json.get("msg");
                if (isShow) ToastUtil.showToast(msg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取版本名称
     */
    public static String getVersion(Context context) {
        String version = "";
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            version = info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return version;
    }

    /**
     * 获取手机制造商
     */
    public static String getOSName() {
        String manufacturer = Build.MANUFACTURER;
        if (manufacturer != null) return manufacturer.toLowerCase();
        else return "";
    }

    /**
     * 判断app是否在前台
     */
    public static boolean isAppForeground(Context context, String packageName) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
        String currentPackageName = cn.getPackageName();
        return !TextUtils.isEmpty(currentPackageName)
                && currentPackageName.equals(packageName);
    }

}
